﻿#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    picwin->setAttribute(Qt::WA_DeleteOnClose);
}

MainWindow::~MainWindow()
{
    delete ui;
}

int MainWindow::OpenImageFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, "open Image", "./debug", "Image File(*.bmp *.jpg *.jpeg *.png)");
    if (fileName.size()){
        QTextCodec* code = QTextCodec::codecForName("gb18030");
        std::string name = code->fromUnicode(fileName).data();
        auto m_srcImage = cv::imread(name);

        if (m_srcImage.data)
        {
            cvtColor(m_srcImage, m_srcImage, cv::COLOR_BGR2RGB);//BGR转化为RGB
            QImage::Format format = QImage::Format_RGB888;
            switch (m_srcImage.type())
            {
            case CV_8UC1:
                format = QImage::Format_Indexed8;
                break;
            case CV_8UC3:
                format = QImage::Format_RGB888;
                break;
            case CV_8UC4:
                format = QImage::Format_ARGB32;
                break;
            }
            img = QImage((const uchar*)m_srcImage.data, m_srcImage.cols, m_srcImage.rows,
                m_srcImage.cols * m_srcImage.channels(), format);

            cv::cvtColor(m_srcImage, imgHsv, cv::COLOR_RGB2HSV);
            qimghsv = QImage((const uchar*)imgHsv.data, imgHsv.cols, imgHsv.rows,
                imgHsv.cols * imgHsv.channels(), format);

            picwin->recvShowPicSignal(img);
            picwin->recvShowPicSignalHSV(qimghsv);
        }
    }
    return fileName.size();
}



void MainWindow::on_pushButton_clicked()
{
    if(OpenImageFile()){
        picwin->resize(800*2.05,600);
        picwin->move(this->pos().x()-abs((picwin->width()-this->width())/2),0);
    //    picwin.OpenImageFile(path);
    //    picwin.setToolTip("原图");
    //    picwin.recvShowPicSignal(img);
        picwin->show();
    }
}


void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    if(ui->spinBox->value()==value)return;
    ui->spinBox->setValue(value);
    PicShuaXin();
}


void MainWindow::on_spinBox_valueChanged(int arg1)
{
    if(ui->horizontalSlider->value()==arg1)return;
    ui->horizontalSlider->setValue(arg1);
}


void MainWindow::on_horizontalSlider_2_valueChanged(int value)
{
    if(ui->spinBox_2->value()==value)return;
    ui->spinBox_2->setValue(value);
    PicShuaXin();
}


void MainWindow::on_spinBox_2_valueChanged(int arg1)
{
    if(ui->horizontalSlider_2->value()==arg1)return;
    ui->horizontalSlider_2->setValue(arg1);
}


void MainWindow::on_horizontalSlider_3_valueChanged(int value)
{
    if(ui->spinBox_3->value()==value)return;
    ui->spinBox_3->setValue(value);
    PicShuaXin();
}


void MainWindow::on_spinBox_3_valueChanged(int arg1)
{
    if(ui->horizontalSlider_3->value()==arg1)return;
    ui->horizontalSlider_3->setValue(arg1);
}


void MainWindow::on_horizontalSlider_4_valueChanged(int value)
{
    if(ui->spinBox_4->value()==value)return;
    ui->spinBox_4->setValue(value);
    PicShuaXin();
}


void MainWindow::on_spinBox_4_valueChanged(int arg1)
{
    if(ui->horizontalSlider_4->value()==arg1)return;
    ui->horizontalSlider_4->setValue(arg1);
}


void MainWindow::on_horizontalSlider_5_valueChanged(int value)
{
    if(ui->spinBox_5->value()==value)return;
    ui->spinBox_5->setValue(value);
    PicShuaXin();
}


void MainWindow::on_spinBox_5_valueChanged(int arg1)
{
    if(ui->horizontalSlider_5->value()==arg1)return;
    ui->horizontalSlider_5->setValue(arg1);
}


void MainWindow::on_horizontalSlider_6_valueChanged(int value)
{
    if(ui->spinBox_6->value()==value)return;
    ui->spinBox_6->setValue(value);
    PicShuaXin();
}


void MainWindow::on_spinBox_6_valueChanged(int arg1)
{
    if(ui->horizontalSlider_6->value()==arg1)return;
    ui->horizontalSlider_6->setValue(arg1);
}

void MainWindow::PicShuaXin(){
    if (picwin->isVisible()){
        lower = cv::Scalar(ui->horizontalSlider->value(), ui->horizontalSlider_3->value(),ui->horizontalSlider_5->value());
        upper = cv::Scalar(ui->horizontalSlider_2->value(),ui->horizontalSlider_4->value(),ui->horizontalSlider_6->value());
        cv::inRange(imgHsv, lower, upper,mask);
        qimghsv = QImage((const uchar*)mask.data, mask.cols, mask.rows,
            mask.cols * mask.channels(), QImage::Format_Indexed8);
        picwin->recvShowPicSignalHSV(qimghsv);
    }
}
